package com.boyaa.shells.tool 
{
	import com.boyaa.fly.Timepiece;
	import com.boyaa.shells.tool.OnScroll;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.events.Event;
	/**
	 * ...代理控制文本域的滚动
	 * @author henrylee   Contact me henrylee_sz@qq.com.
	 */
	public class ScrollProxy 
	{
		private var _aim: DisplayObject;
		private var _mask: DisplayObject;
		private var _scrollBarRaw: MovieClip;
		
		private var _scroll: OnScroll;
		
		/**
		 * 滚动显示控制类
		 * @param	aim                   需要滚动条的显示对象
		 * 						    该显示对象需要预先设定好mask
		 * 						    e.g.     aim.mask = goalMask;
		 *                                                      _scrollGoal= new ScrollProxy(aim, scrollBarRaw);
		 * @param	scrollBarRaw      滚动条资源
		 */
		public function ScrollProxy(aim:DisplayObject, scrollBarRaw: MovieClip) 
		{
			_aim = aim;
			_scrollBarRaw = scrollBarRaw;
			_mask = aim.mask;
			
			init();
		}
		
		private function init():void
		{
			_scroll = new OnScroll(_scrollBarRaw, onScroll);
			_scroll._top = 16;
			_scroll._length = _scrollBarRaw.height - 32;
		}
		private function onScroll(num:Number):void
		{
			var len:Number = _aim.height - _mask.height;
			_aim.y = -len * num + _mask.y;
		}
		
		
		public function addEvent():void
		{
			_scroll.addEvent();
		}
		
		public function onViewChange():void 
		{
			_aim.y = _mask.y;
			var num:Number = _mask.height / _aim.height;
			_scrollBarRaw.visible = (num < 1);
			if (num < 1) _scroll.setScale(num);
		}
		
		public function removeEvent():void
		{
			_scroll.removeEvent();
		}
		
	}

}